/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Controllers;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import java.util.Vector;

import Models.ThoiDiem_Model;
import Beans.NhanVien;
import Models.NhatKyHT_Model;

/**
 *
 * @author Venus
 */
public class ThoiDiem_Controller extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        try {
            if (session.getAttribute("langid") == null) {
                response.sendRedirect("index.jsp");
                return;
            }
            session.removeAttribute("DSMA");
            session.removeAttribute("DSNMA");
            session.removeAttribute("item");
            String action = request.getParameter("action");
            int langid = (Integer) session.getAttribute("langid");
            if (action.equals("Show")) {
                String item = request.getParameter("item");
                ThoiDiem_Model tdm = new ThoiDiem_Model();
                Vector vDSTD = new Vector();
                vDSTD = tdm.LayDSTD(langid);
                request.setAttribute("DSTD", vDSTD);
                request.setAttribute("item", item);
                RequestDispatcher rd = request.getRequestDispatcher("admin/newtime.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("Add")) {
                String timename = request.getParameter("timename");
                String tugio = request.getParameter("tugio");
                String tuphut = request.getParameter("tuphut");
                String dengio = request.getParameter("dengio");
                String denphut = request.getParameter("denphut");
                int MaTD = ThoiDiem_Model.TaoMa();
                ThoiDiem_Model.AddTime(MaTD, timename, tugio, tuphut, dengio, denphut);
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                NhatKyHT_Model.GhiNhatKy(2, nv.getMaNV(), "time");
                ThoiDiem_Model tdm = new ThoiDiem_Model();
                Vector v = new Vector();
                v = tdm.LayTime(MaTD);
                request.setAttribute("TimeLang", v);
                RequestDispatcher rd = request.getRequestDispatcher("admin/timelang.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("ShowTD")) {
                String item = request.getParameter("item");
                ThoiDiem_Model tdm = new ThoiDiem_Model();
                Vector v = new Vector();
                v = tdm.LayDSTD(langid);
                request.setAttribute("DSTD", v);
                request.setAttribute("item", item);
                RequestDispatcher rd = request.getRequestDispatcher("admin/listtime.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("Edit")) {
                String tentd = request.getParameter("tentd");
                String tugio = request.getParameter("tugio");
                String tuphut = request.getParameter("tuphut");
                String dengio = request.getParameter("dengio");
                String denphut = request.getParameter("denphut");
                String matd = request.getParameter("matd");
                String mann = request.getParameter("mann");
                ThoiDiem_Model.Edit(tentd, tugio, tuphut, dengio, denphut, matd, mann);
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                NhatKyHT_Model.GhiNhatKy(3, nv.getMaNV(), "timeinfo");
                ThoiDiem_Model tdm = new ThoiDiem_Model();
                Vector v = new Vector();
                v = tdm.LayTime(Integer.parseInt(matd));
                request.setAttribute("TimeLang", v);
                RequestDispatcher rd = request.getRequestDispatcher("admin/timelang.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("ShowLangTime")) {
                String MaTD = request.getParameter("MaTD");
                ThoiDiem_Model tdm = new ThoiDiem_Model();
                Vector v = new Vector();
                v = tdm.LayTime(Integer.parseInt(MaTD));
                request.setAttribute("TimeLang", v);
                RequestDispatcher rd = request.getRequestDispatcher("admin/timelang.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("UpdateStatusTime")) {
                String matd = request.getParameter("matd");
                String status = request.getParameter("status");
                String item = request.getParameter("item");
                ThoiDiem_Model.UpdateStatusTime(matd, status);
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                NhatKyHT_Model.GhiNhatKy(3, nv.getMaNV(), "statustime");
                ThoiDiem_Model tdm = new ThoiDiem_Model();
                Vector v = new Vector();
                v = tdm.LayDSTD(langid);
                request.setAttribute("DSTD", v);
                request.setAttribute("item", item);
                RequestDispatcher rd = request.getRequestDispatcher("admin/listtime.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("Search")) {
                String tentd = request.getParameter("tentd");
                String tinhtrang = request.getParameter("tinhtrang");
                String item = request.getParameter("item");
                ThoiDiem_Model tdm = new ThoiDiem_Model();
                Vector v = new Vector();
                v = tdm.Search(tentd, tinhtrang, langid);
                request.setAttribute("tentd", tentd);
                request.setAttribute("tinhtrang", tinhtrang);
                request.setAttribute("item", item);
                request.setAttribute("DSTD", v);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssearchtime.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortTenTD")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                ThoiDiem_Model tdm = new ThoiDiem_Model();
                Vector v = new Vector();
                v = tdm.SortTenTD(langid, order);
                request.setAttribute("DSTD", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "1");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssorttime.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortTTTD")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                ThoiDiem_Model tdm = new ThoiDiem_Model();
                Vector v = new Vector();
                v = tdm.SortTTTD(langid, order);
                request.setAttribute("DSTD", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "2");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssorttime.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortSearchTenTD")) {
                String tentd = request.getParameter("tentd");
                String tinhtrang = request.getParameter("tinhtrang");
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                ThoiDiem_Model tdm = new ThoiDiem_Model();
                Vector v = new Vector();
                v = tdm.SortSearchTenTD(tentd, tinhtrang, langid, order);
                request.setAttribute("DSTD", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "1");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                request.setAttribute("tentd", tentd);
                request.setAttribute("tinhtrang", tinhtrang);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortsearchtime.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortSearchTTTD")) {
                String tentd = request.getParameter("tentd");
                String tinhtrang = request.getParameter("tinhtrang");
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                ThoiDiem_Model tdm = new ThoiDiem_Model();
                Vector v = new Vector();
                v = tdm.SortSearchTTTD(tentd, tinhtrang, langid, order);
                request.setAttribute("DSTD", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "2");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                request.setAttribute("tentd", tentd);
                request.setAttribute("tinhtrang", tinhtrang);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortsearchtime.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("CheckTime")) {
                String tugio = request.getParameter("tugio");
                String tuphut = request.getParameter("tuphut");
                String dengio = request.getParameter("dengio");
                String denphut = request.getParameter("denphut");
                if(ThoiDiem_Model.CheckTime(tugio, tuphut, dengio, denphut, langid)){
                    out.print("true");
                }else{
                    out.print("false");
                }
            }
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
